#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <QString>
#include <QFileInfo>

#include "playlistentry.h"

class TagReaderThread;

class Playlist : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(Playlist)

public:
    enum PlaylistType
    {
        Unknown,
        M3U,
        MPL,
    };

    Playlist(const QString& i_filepath, PlaylistType i_type, QObject* i_pParent = NULL);
    ~Playlist();

    inline const QFileInfo& fileInfo() const {return m_fileInfo;}
    inline const QString& filename() const {return m_filename;}
    inline int songCount() const {return m_entries.size();}
    inline int missingCount() const {return m_missingCount;}
    inline int duplicateCount() const {return m_duplicateCount;}
    inline int incompleteCount() const {return m_incompleteCount;}
    inline const QList<PlaylistEntry*>& entries() const {return m_entries;}
    inline bool hasChanges() const {return m_hasChanges;}
    inline PlaylistType type() const {return m_type;}

    QString typeString() const;

    void checkAudioFiles(const QHash<QString, AudioFile*> &i_audioFiles);
    void addAudioFiles(const QList<AudioFile*>& i_audioFiles);
    bool save();
    void removeEntry(int i_idx);
    bool setFilepath(const QString &i_path);
    void setChanged();

    static Playlist* parse(const QString& i_filepath, const QHash<QString, AudioFile*> &i_audioFiles);
    static PlaylistType getTypeFromFilename(const QString& i_filename);    

signals:
    void dataChanged();

public:
    static QString sm_replacePath;
    static QString sm_localPath;
    static TagReaderThread* sm_pTagThread;

private:
    bool parse(QFile& i_file, const QHash<QString, AudioFile*> &i_audioFiles);
    void checkMissing();

private slots:
    void adjustCompleteState(bool i_isComplete);

private:
    QFileInfo m_fileInfo;
    QString m_filename;
    PlaylistType m_type;
    int m_missingCount;
    int m_duplicateCount;
    int m_incompleteCount;
    bool m_hasChanges;
    QList<PlaylistEntry*> m_entries;
};

#endif // PLAYLIST_H
